@ECHO OFF
REM Reset to current working directory in case run with administrator privilege
CD /d "%~dp0"

REM -----------------------
REM Installer for sql scripts; this script calls _execute-sql-scripts.bat
REM with the given credentials, and _execute-sql-scripts.bat executes each set of sql
REM scripts is a directory
REM -----------------------

:START
CLS
ECHO ================================================================
SET /P SERVER=Enter the SQL Server name: 
SET /P USERNAME=Enter the User name: 
SET /P PASSWORD=Enter the Password for '%USERNAME%': 
ECHO.

ECHO == Please verify the following:
ECHO == 
ECHO == .... Host/Server Name   : %SERVER%
ECHO == .... User Name          : %USERNAME%
ECHO ==
ECHO.

SET /P CHOICE=Continue? (Y/N): 
IF NOT "%CHOICE%" == "Y" (
	IF NOT "%CHOICE%" == "y" (
		ECHO.
		GOTO START
	)
)
ECHO.

SETLOCAL EnableDelayedExpansion

:: Get datetime
SET X=
FOR /f "skip=1 delims=" %%x IN ('wmic os get localdatetime') DO IF NOT DEFINED X SET X=%%x
SET YEAR=%X:~0,4%
SET MONTH=%X:~4,2%
SET DAY=%X:~6,2%
SET HOUR=%X:~8,2%
SET MINUTE=%X:~10,2%
SET SECOND=%X:~12,2%
SET _datetime=%YEAR%%MONTH%%DAY%_%HOUR%-%MINUTE%-%SECOND%

SETLOCAL DisableDelayedExpansion

IF NOT EXIST "Scripts" (
    ECHO Scripts folder does not exist.
    GOTO END
)
IF NOT EXIST "Logs" MD "Logs"
SET LOGFILE=Logs\Logs_%_datetime%.log
FOR /f "delims=" %%i in ('dir Scripts /ad/s/b') DO (
    CALL Scripts\_execute-sql-scripts.bat %SERVER% %USERNAME% %PASSWORD% "%%i"  >>  %LOGFILE%
)

ECHO == ======================================================= ==
ECHO ==                      !! WARNING !!                      ==
ECHO ==                                                         ==
ECHO ==  Please view the log file for any errors to check       ==
ECHO ==  if the scripts ran successfully or failed.             ==
ECHO ==                                                         ==
ECHO ==  Log FileName : %LOGFILE%         ==
ECHO ==                                                         ==
ECHO == ======================================================= ==
ECHO.

SET /P CHOICE=Do you want to view the log file? (Y/N): 
IF "%CHOICE%" == "Y" START notepad "%LOGFILE%"
IF "%CHOICE%" == "y" START notepad "%LOGFILE%"
GOTO END

:END
PAUSE
